import Vue from 'vue'
import Vuex from 'vuex'
import { getItem, setItem } from '../utils/storage'

Vue.use(Vuex)

const TOKEN_KEY = 'TOKEN_KEY'

export default new Vuex.Store({
  state: {
    // 存储当前登录的用户信息
    user: getItem(TOKEN_KEY),
    // user: JSON.parse(window.localStorage.getItem(TOKEN_KEY)),
    // user: null,
  },
  mutations: {
    // 对 user 进行修改
    setUser(state, data) {
      state.user = data;
      // 为了方式刷新丢失 token 数据，我们需要把数据持久化存储到本地存储
      setItem(TOKEN_KEY, state.user)
      // window.localStorage.setItem(TOKEN_KEY, JSON.stringify(state.user));
    }
  },
  actions: {
  },
  modules: {
  }
})
